package util;
/**
  * The Timer class allows a graceful exit when an application
  * is stalled due to a networking timeout. Once the timer is
  * set, it must be cleared via the reset() method, or the
  * timeout() method is called.
  * <p>
  * The timeout length is customizable, by changing the 'length'
  * property, or through the constructor. The length represents
  * the length of the timer in milliseconds.
  *
  * @author	David Reilly
  */
public class Timer extends Thread
{
	/** Rate at which timer is checked */
	protected int m_rate = 100;

	/** Length of timeout */
	private int m_length;

	/** Time elapsed */
	private int m_elapsed;

    private boolean ok;
    public String resource;
	/**
	  * Creates a timer of a specified length
	  * @param	length	Length of time before timeout occurs
	  */
	public Timer ( int length )
	{
		// Assign to member variable
		m_length = length;

		reset();
	}


	/** Resets the timer back to zero */
	public synchronized void reset()
	{
		m_elapsed = -1;
        ok = true;
	}

    public synchronized void begin()
    {
        m_elapsed=0;
    }

	/** Performs timer specific code */
    @Override
	public void run()
	{
		// Keep looping
		for (;;)
		{
			// Put the timer to sleep
			try
			{
				Thread.sleep(m_rate);
			}
			catch (InterruptedException ioe)
			{
                System.out.println("PROBLME");
				continue;
			}

			// Use 'synchronized' to prevent conflicts
			synchronized ( this )
			{
				// Increment time remaining if we are active
				if(m_elapsed!=-1)
                    m_elapsed += m_rate;
				// Check to see if the time has been exceeded
				if (m_elapsed > m_length)
				{
					// Trigger a timeout
                    ok = false;
				}
			}

		}
	}


    public boolean ok(){
        return ok;
    }

	// Override this to provide custom functionality
	public void timeout() throws Exception
	{

	}
}